package com.lzh.chatnote.user.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.lzh.chatnote.common.utils.UserUtils;
import com.lzh.chatnote.user.dao.UserBackpackDao;
import com.lzh.chatnote.user.dao.UserDao;
import com.lzh.chatnote.user.domain.dto.UserSummaryDTO;
import com.lzh.chatnote.user.domain.entity.ItemConfig;
import com.lzh.chatnote.user.domain.entity.User;
import com.lzh.chatnote.user.domain.entity.UserBackpack;
import com.lzh.chatnote.user.domain.enums.ItemTypeEnum;
import com.lzh.chatnote.user.domain.vo.request.QueryBatchUserReq;
import com.lzh.chatnote.user.domain.vo.response.user.BadgeResp;
import com.lzh.chatnote.user.domain.vo.response.user.SummaryInfoResp;
import com.lzh.chatnote.user.service.UserService;
import com.lzh.chatnote.user.service.adapter.UserAdapter;
import com.lzh.chatnote.user.service.cache.ItemCache;
import com.lzh.chatnote.user.service.cache.UserSummaryCache;
import com.lzh.chatnote.user.service.event.UserRegisterEvent;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;
    @Resource
    private ItemCache itemCache;
    @Resource
    private UserBackpackDao userBackpackDao;
    @Resource
    private UserAdapter userAdapter;
    @Resource
    private ApplicationEventPublisher applicationEventPublisher;
    @Resource
    private UserSummaryCache userSummaryCache;

    @Override
    public void registerUser(User user) {
        //保存用户
        userDao.save(user);
        //todo 给注册用户发送改名卡等东西
        //观察者模式，1.发放改名卡，发送邮箱等
        applicationEventPublisher.publishEvent(new UserRegisterEvent(this, user));
    }

    @Override
    public List<BadgeResp> badges() {
        //使用sa-token获取用户ID
        Long uuid = UserUtils.getCurrentUserId();
        //获取所有的徽章
        List<ItemConfig> allBadgeItems = itemCache.getByType(ItemTypeEnum.BADGE.getType());

        Set<Long> allBadgeIds = allBadgeItems.stream().map(ItemConfig::getId).collect(Collectors.toSet());
        //获取当前用户拥有的徽章
        List<UserBackpack> containBadges = userBackpackDao.containBadge(uuid, ItemTypeEnum.BADGE.getType(), allBadgeIds);
        //获取当前用户
        User user = userDao.getById(uuid);
        //build
        return userAdapter.buildWearingBadge(allBadgeItems, containBadges, user);
    }

    @Override
    public List<SummaryInfoResp> userInfoBatch(QueryBatchUserReq queryBatchUserReq) {
        //需要刷新的用户Ids
        List<Long> needAsyncUids = getNeedAsyncUserIds(queryBatchUserReq.getReqList());
        //获取用户简介信息
        Map<Long, UserSummaryDTO> mapUserSummary = userSummaryCache.getBatch(needAsyncUids);
        return queryBatchUserReq.getReqList()
                .stream().map(item -> {
                    UserSummaryDTO userSummaryDTO = mapUserSummary.get(item.getUid());
                    if (Objects.isNull(userSummaryDTO)) {
                        //不需要刷新的数据
                        return SummaryInfoResp.skipRefresh(item.getUid());
                    }
                    SummaryInfoResp summaryInfoResp = new SummaryInfoResp();
                    BeanUtils.copyProperties(userSummaryDTO, summaryInfoResp);
                    return summaryInfoResp;
                }).collect(Collectors.toList());
    }

    private List<Long> getNeedAsyncUserIds(List<QueryBatchUserReq.UserInfoReq> reqList) {
        return null;
    }
}
